﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using Microhard.Permission.Models;
using AutoMapper;
using Microhard.Core.DBUtility;
using System.Web.Profile;

namespace Microhard.Permission_MembershipImpl
{
    public static class PermissionMapExtension
    {
        #region User
        public static MembershipUser ToEntity(this User user)
        {
            return Mapper.Map<MembershipUser>(user);
        }

        public static aspnet_User ToDBEntity(this User user)
        {
            return Mapper.Map<aspnet_User>(user);
        }


        public static User ToModel(this MembershipUser entity)
        {
            var model = Mapper.Map<User>(entity);
            return model;
        }

        public static User ToModel(this aspnet_User entity)
        {
            var user = Mapper.Map<User>(entity);
            user.Roles = entity.aspnet_UsersInRoles.Select(uir => uir.aspnet_Role.ToModel()).ToList();
            var pb = ProfileBase.Create(entity.UserName);
            if (pb.GetPropertyValue("Address") != null)
            {
                user.Address = pb.GetPropertyValue("Address").ToString();
            }
            if (pb.GetPropertyValue("CellPhone") != null)
            {
                user.CellPhone = pb.GetPropertyValue("CellPhone").ToString();
            }
            if (pb.GetPropertyValue("Phone") != null)
            {
                user.Phone = pb.GetPropertyValue("Phone").ToString();
            }
            if (pb.GetPropertyValue("Duty") != null)
            {
                user.Duty = pb.GetPropertyValue("Duty").ToString();
            }
            if (pb.GetPropertyValue("Department") != null)
            {
                user.Department = pb.GetPropertyValue("Department").ToString();
            }
            if (pb.GetPropertyValue("Mark") != null)
            {
                user.Mark = pb.GetPropertyValue("Mark").ToString();
            }
            if (pb.GetPropertyValue("UserRealName") != null)
            {
                user.UserRealName = pb.GetPropertyValue("UserRealName").ToString();
            }
            user.AccountMappingEmployee = bool.Parse(pb.GetPropertyValue("AccountMappingEmployee").ToString());
            return user;
        }

        public static IEnumerable<MembershipUser> ToEntity(this IEnumerable<User> users)
        {
            return Mapper.Map<IEnumerable<MembershipUser>>(users);
        }

        public static IEnumerable<User> ToModel(this  IEnumerable<MembershipUser> enities)
        {
            return Mapper.Map<IEnumerable<User>>(enities);
        }
        #endregion

        #region Resource
        public static MicroHard_Resource ToEntity(this Resouce model)
        {
            return Mapper.Map<MicroHard_Resource>(model);
        }

        public static Resouce ToModel(this MicroHard_Resource entity)
        {
            var model = Mapper.Map<Resouce>(entity);
            model.ResourceType = entity.MicroHard_ResourceType.ToModel();
            if (entity.ResourcesChild != null && entity.ResourcesChild.Count > 0)
            {
                model.SubResources = entity.ResourcesChild.Select(r => r.ToModel()).ToList();
            }
            if (entity.ResourcesParent != null)
            {
                model.ParentResourceID = entity.ResourcesParent.ResourceID;
                model.ParentResouceName = entity.ResourcesParent.Name;
            }
            if (entity.MicroHard_ResourceType != null)
            {
                model.ResourceTypeID = entity.MicroHard_ResourceType.ResourceTypeID;
                model.ResourceTypeName = entity.MicroHard_ResourceType.Name;
            }
            if (entity.MicroHard_Group != null)
            {
                model.AllowGroupID = entity.MicroHard_Group.OrgID;
                model.AllowGroupName = entity.MicroHard_Group.OrgName;
            }
            return model;
        }
        #endregion

        #region ResourceType
        public static MicroHard_ResourceType ToEntity(this ResouceType model)
        {
            return Mapper.Map<MicroHard_ResourceType>(model);
        }

        public static ResouceType ToModel(this MicroHard_ResourceType entity)
        {
            var model = Mapper.Map<ResouceType>(entity);
            return model;
        }
        #endregion

        #region Role
        public static aspnet_Role ToEntity(this Role model)
        {
            return Mapper.Map<aspnet_Role>(model);
        }

        public static Role ToModel(this aspnet_Role entity)
        {
            var model = Mapper.Map<Role>(entity);
            return model;
        }
        #endregion

        #region Permission
        public static Microhard.Permission.Models.Permission ToModel(this MicroHard_Permission entity)
        {
            var model = Mapper.Map<Microhard.Permission.Models.Permission>(entity);
            model.BelongGroup = entity.MicroHard_Resource.MicroHard_Group.OrgName;
            model.ResourceKeyName = entity.MicroHard_Resource.KeyName;
            model.OperationKeyName = entity.MicroHard_Operation.KeyName;
            return model;
        }

        public static MicroHard_Permission ToEntity(this Microhard.Permission.Models.Permission model)
        {
            var entity = Mapper.Map<MicroHard_Permission>(model);
            return entity;
        }
        #endregion

        #region Operation
        public static MicroHard_Operation ToEntity(this Operation model)
        {
            return Mapper.Map<MicroHard_Operation>(model);
        }

        public static Operation ToModel(this MicroHard_Operation entity)
        {
            var model = Mapper.Map<Operation>(entity);
            return model;
        }

        public static MicroHard_Group ToEntity(this Group model)
        {
            return Mapper.Map<MicroHard_Group>(model);
        }
        #endregion

        #region Group
        public static Group ToModel(this MicroHard_Group entity)
        {
            var model = Mapper.Map<Group>(entity);
            return model;
        }

        public static IEnumerable<Group> ToModel(this  IEnumerable<MicroHard_Group> enities)
        {
            return Mapper.Map<IEnumerable<Group>>(enities);
        }
        #endregion

        #region DataCollection
        public static PermissionDataCollection ToModel(this MicroHard_DataCollection entity)
        {
            var model = Mapper.Map<PermissionDataCollection>(entity);
            return model;
        }

        public static MicroHard_DataCollection ToEntity(this PermissionDataCollection model)
        {
            return Mapper.Map<MicroHard_DataCollection>(model);
        }
        #endregion

        #region DataPermission
        public static DataPermission ToModel(this MicroHard_DataPermission entity)
        {
            var model = Mapper.Map<DataPermission>(entity);
            return model;
        }

        public static MicroHard_DataPermission ToEntity(this DataPermission model)
        {
            return Mapper.Map<MicroHard_DataPermission>(model);
        }
        #endregion

        #region Dictionary
        public static DictionaryModel ToModel(this MicroHard_Dictionary entity)
        {
            var model = Mapper.Map<DictionaryModel>(entity);
            return model;
        }

        public static MicroHard_Dictionary ToEntity(this DictionaryModel model)
        {
            return Mapper.Map<MicroHard_Dictionary>(model);
        }
        #endregion

        #region UserOperationLog
        public static UserOperationLogModel ToModel(this Microhard_UserOperationLog entity)
        {
            var model = Mapper.Map<UserOperationLogModel>(entity);
            return model;
        }

        public static Microhard_UserOperationLog ToEntity(this UserOperationLogModel model)
        {
            return Mapper.Map<Microhard_UserOperationLog>(model);
        }
        #endregion
    }
}
